package com.zzyq.pc.mongodb.Service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.common.plugin.shiro.TokenManager;
import com.zzyq.pc.mongodb.Service.IWarningService;
import com.zzyq.pc.mongodb.base.Basics;
import com.zzyq.pc.mongodb.dao.MongodbWarning;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.web.configure.base.Menu;
import com.zzyq.pc.web.configure.mapper.EnshrineMapper;
import com.zzyq.pc.web.configure.mapper.InformationMapper;
import com.zzyq.pc.web.configure.model.Information;

@Service
public class WarningService implements IWarningService{
	MongodbWarning mongodb = new MongodbWarning();
	
	@Autowired
	EnshrineMapper enshrineMapper;
	@Autowired
	InformationMapper informationMapper;
	
	//转载数和星级类
	Basics bs = new Basics();
	
	/**
	 * 舆情预警——首页
	 * @param MongodbDate 对象名
	 * @return
	 */
	@Override
	public List<MongodbDate> findWarning(Integer greaterForward,Date timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map);
		//拿到敏感方案
		for (Information in : info) {
			if(in.getIfmIssensitivity()==1)
			ifmIds.add(String.valueOf(in.getIfmId()));
		}
		if (ifmIds.size() <= 0) {
			return list;
		}
		list = mongodb.findWarning(MongodbDate.class, ifmIds,greaterForward,timeScope);
		for (MongodbDate date : list) {
			//判断星级
			if (date.getReprintNumb() > greaterForward) {
				//五星
				date.setStarLevel(bs.getFiveStar());
			}
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()).text()+Jsoup.parse(date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	/**
	 * 高危舆情——首页
	 * @param MongoDate 对象名
	 * @return
	 */
	@Override
	public List<MongodbDate> findRisk(Integer greaterForward,Integer lessForward,Date timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map);
		//拿到敏感方案
		for (Information com : info) {
			if(com.getIfmIssensitivity()==1)
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds.size() <= 0) {
			return list;
		}
		list = mongodb.findRisk(MongodbDate.class, ifmIds,greaterForward,lessForward,timeScope);
		for (MongodbDate date : list) {
			//判断是否为媒体
			if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
				//四星
				date.setStarLevel(bs.getFourStar());
			} else {
				//判断星级
				if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
					//四星
					date.setStarLevel(bs.getFourStar());
				} else if(date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
					//三星
					date.setStarLevel(bs.getThreeStar());
				}
			}
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()).text()+Jsoup.parse(date.getWeiboSourceContent()));
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		for (int i = 0; i < dtlist.size(); i++) {
			if (!dtlist.get(i).getArticleType().equals(Menu.News.name())) {
				//排序  集合中只要是新闻数据，排在最前面
				Collections.sort(dtlist, new Comparator<MongodbDate>() {
					@Override
					public int compare(MongodbDate date1, MongodbDate date2) {
						if (date1.getArticleType().equalsIgnoreCase(Menu.News.name())) {
							return -1;
						}
						return 0;
					}
				});
			}
		}
		return dtlist;
	}
	
	/**
	 * 负面舆情——首页
	 * @param MongoDate 对象名
	 * @return
	 */
	@Override
	public List<MongodbDate> findNegative(Integer lessForward,Date timeScope){
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map);
		//拿到敏感方案ID
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				ifmIds.add(String.valueOf(com.getIfmId()));
			}
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		list = mongodb.findNegative(MongodbDate.class,ifmIds,lessForward,timeScope);
		for (MongodbDate date : list) {
			//判断星级
			if (date.getReprintNumb()<bs.getFigureFive()) {
				//一星
				date.setStarLevel(bs.getOneStar());
			} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
				//两星
				date.setStarLevel(bs.getTwoStar());
			}
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()).text()+Jsoup.parse(date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	/**
	 * 最新舆情——首页
	 * @param MongoDate 对象
	 */
	@Override
	public List<MongodbDate> findNewest(Date timeScope){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//敏感方案集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map);
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			}
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		list = mongodb.findNewest(MongodbDate.class, ifmIds,timeScope);
		for (MongodbDate date : list) {
			for (String ids : ifmIds2) {
				if (date.getIfmId().equals(ids)) {
					if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
						//四星
						date.setStarLevel(bs.getFourStar());
					} else {
						//判断星级
						if (date.getReprintNumb()<bs.getFigureFive()) {
							//一星
							date.setStarLevel(bs.getOneStar());
						} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
							//两星
							date.setStarLevel(bs.getTwoStar());
						} else if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
							//三星
							date.setStarLevel(bs.getThreeStar());
						} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
							//四星
							date.setStarLevel(bs.getFourStar());
						} else if (date.getReprintNumb()>=bs.getFigureTwenty()) {
							//五星
							date.setStarLevel(bs.getFiveStar());
						}
					}
				}
			}
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()).text()+Jsoup.parse(date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	
	/**
	 * 舆情预警——更多
	 * @param MongodbDate 对象名	
	 * @param ifmIds 方案ID集合
	 * @param pageView 分页参数
	 * @return
	 */
	@Override
	public List<MongodbDate> findWarningMore(PageView pageView,Integer greaterForward){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			}
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds2.size()<=0) {
			return list;
		}
		list = mongodb.findWarningMore(MongodbDate.class, ifmIds2, pageView,greaterForward);
		for (MongodbDate date : list) {
			date.setNature(Menu.Nature2.getName());
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			//结果集只有五星的数据， 判断加入星星
			if (date.getReprintNumb()>greaterForward) {
				//五星
				date.setStarLevel(bs.getFiveStar());
			}
			//判断是否为微博数据
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()).text()+Jsoup.parse(date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	
	/**
	 * 负面舆情——更多
	 * @param MongoDate 对象名
	 * @param ifmIds	方案ID集合
	 * @param pageView	分页参数
	 * @return
	 */
	@Override
	public List<MongodbDate> findNegativeMore(PageView pageView,Integer lessForward){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团队ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			}
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds2.size()<=0) {
			return list;
		}
		list = mongodb.findNegativeMore(MongodbDate.class, ifmIds2, pageView, lessForward);
		for (MongodbDate date : list) {
			date.setNature(Menu.Nature2.getName());
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			// 结果集只有一星和两星的数据， 判断加入星星
			if (date.getReprintNumb() < bs.getFigureFive()) {
				// 一星
				date.setStarLevel(bs.getOneStar());
			} else if (date.getReprintNumb() >= bs.getFigureFive() && date.getReprintNumb() < bs.getFigureTen()) {
				// 两星
				date.setStarLevel(bs.getTwoStar());
			}
			//判断是否为微博数据
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	/**
	 * 最新舆情——更多
	 * @param MongoDate
	 * @param pageView
	 * @param ifmIds
	 * @return
	 */
	@Override
	public List<MongodbDate> findNewestMore(PageView pageView){
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//返回数据集合
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		//方案ID集合
		List<String> ifmIds = new ArrayList<String>();
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information com : info) {
			if (com.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(com.getIfmId()));
			}
			ifmIds.add(String.valueOf(com.getIfmId()));
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		list = mongodb.findNewestMore(MongodbDate.class, pageView, ifmIds);
		for (MongodbDate date : list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			for (String ids : ifmIds2) {
				if (date.getIfmId().equals(ids)) {
					date.setNature(Menu.Nature2.getName());
					if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
						//四星
						date.setStarLevel(bs.getFourStar());
					} else {
						//判断星级
						if (date.getReprintNumb()<bs.getFigureFive()) {
							//一星
							date.setStarLevel(bs.getOneStar());
						} else if (date.getReprintNumb()>=bs.getFigureFive() && date.getReprintNumb()<bs.getFigureTen()) {
							//两星
							date.setStarLevel(bs.getTwoStar());
						} else if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
							//三星
							date.setStarLevel(bs.getThreeStar());
						} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
							//四星
							date.setStarLevel(bs.getFourStar());
						} else if (date.getReprintNumb()>=bs.getFigureTwenty()) {
							//五星
							date.setStarLevel(bs.getFiveStar());
						}
					}
				} else {
					if (null == date.getNature()) {
						date.setNature(Menu.Nature1.getName());
					}
				}
			}
			if (null == date.getNature()) {
				date.setNature(Menu.Nature1.getName());
			}
			//判断是否为微博数据
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		}
		return dtlist;
	}
	
	/**
	 * 高危舆情——更多
	 */
	@Override
	public List<MongodbDate> findHighRiskMore(PageView pageView,Integer greaterForward,Integer lessForward){
		List<MongodbDate> dtlist = new ArrayList<MongodbDate>();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取当前用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		List<String> ifmIds = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information in : info) {
			if (in.getIfmIssensitivity()==1) {
				ifmIds.add(String.valueOf(in.getIfmId()));
			}
		}
		if (ifmIds.size()<=0) {
			return list;
		}
		//根据方案ID查询数据
		list = mongodb.findHighRiskMore(MongodbDate.class, pageView, ifmIds,greaterForward,lessForward);
		for (MongodbDate date : list) {
			date.setNature(Menu.Nature2.getName());
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("articleId", date.getArticleId());
			map.put("ifmId", date.getIfmId());
			map.put("userId", userId);
			//根据方案ID和数据ID查询收藏表
			int count = enshrineMapper.selectByifmIdarticleId(map);
			if (count>0) {
				date.setIsCollection(1);
			} else {
				date.setIsCollection(0);
			}
			if (date.getArticleType().equals(Menu.News.getName()) && date.getReprintNumb()<bs.getFigureTwenty()) {
				//四星
				date.setStarLevel(bs.getFourStar());
			} else {
				//判断星级
				if (date.getReprintNumb()>=bs.getFigureTen() && date.getReprintNumb()<bs.getFigureFifteen()) {
					//三星
					date.setStarLevel(bs.getThreeStar());
				} else if (date.getReprintNumb()>=bs.getFigureFifteen() && date.getReprintNumb()<bs.getFigureTwenty()) {
					//四星
					date.setStarLevel(bs.getFourStar());
				}
			}
			//判断是否为微博数据
			if (date.getArticleType().equals(Menu.Micro.getName())) {
				date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
				date.getTitle().trim();
				date.setAuthor(date.getWeiboBlogger());
			}
			dtlist.add(date);
		} 
		return dtlist;
	} 
	
	
	
	/**
	 * 查询相似数据
	 * @param groupId
	 */
	@Override
	public List<MongodbDate> findSimilarity(String groupId,List<String> idlist,String articleType2,Integer identifying,
			Date endTime1,Date startTime1,String ifmId){
		List<String> ifmIds = new ArrayList<String>();
		List<String> ifmIds2 = new ArrayList<String>();
		List<MongodbDate> list = new ArrayList<MongodbDate>();
		//获取团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information in : info) {
			if (in.getIfmIssensitivity()==1) {
				//敏感方案
				ifmIds.add(String.valueOf(in.getIfmId()));
			}
			//所有方案
			ifmIds2.add(String.valueOf(in.getIfmId()));
		}
		//首页查询相似需要的接口
		if (identifying==1) {
			//查询该团体下的所有敏感方案
			List<MongodbDate> list2 = mongodb.findSimilarity(MongodbDate.class, groupId, ifmIds,idlist,articleType2,
					endTime1,startTime1,null);
			if (list2.size()>0 && null != list) {
				for (MongodbDate date : list2) {
					if (date.getArticleType().equals(Menu.Micro.getName())) {
						date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
						date.getTitle().trim();
						date.setAuthor(date.getWeiboBlogger());
					}
					list.add(date);
				}
			} else {
				return list;
			}
		//监测模块中媒体、网络、和中周搜索查询用的接口
		} else if (identifying==2) {
			//查询该团体下的所有方案
			List<MongodbDate> list2 = mongodb.findSimilarity(MongodbDate.class, groupId, ifmIds2,idlist,articleType2,
					endTime1,startTime1,null);
			if (list2.size()>0 && null != list) {
				for (MongodbDate date : list2) {
					if (date.getArticleType().equals(Menu.Micro.getName())) {
						date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
						date.getTitle().trim();
						date.setAuthor(date.getWeiboBlogger());
					}
					list.add(date);
				}
			} else {
				return list;
			}
		//监测模块中舆情概况查询接口
		} else {
			List<MongodbDate> list3 = mongodb.findSimilarity(MongodbDate.class, groupId, null, idlist, articleType2, endTime1, startTime1,ifmId);
			for (MongodbDate date : list3) {
				if (date.getArticleType().equals(Menu.Micro.name())) {
					date.setTitle(Jsoup.parse(date.getWeiboContent()+date.getWeiboSourceContent()).text());
					date.getTitle().trim();
					date.setAuthor(date.getWeiboBlogger());
				}
				list.add(date);
			}
		}
		return list;
	}
	
	
}
